package com.demo.activity;


import java.util.ArrayList;
import java.util.List;

import com.demo.common.model.Activity;
import com.demo.common.model.FormConfig;
import com.demo.common.model.FormRecord;
import com.jfinal.kit.JsonKit;

public class ActivityService {
	public static final ActivityService me = new ActivityService();
	
	public List registration(String activityId) {
		List<FormConfig> formConfigs = FormConfig.dao.find("select * from form_config where formId=?",activityId);
		return formConfigs;
	}
	
	public void doRegistration(String activityId ,String value) {
		new FormRecord().set("formId", activityId).set("value", value).save();
	}

	public List<List> getRegistrationContents(String activityId) {
		List<FormRecord> lists= FormRecord.dao.find("select f.* from form_record f where f.formId =?",activityId);
		List<List> rows = new ArrayList<List>();
		for(FormRecord formRecord: lists){
			String value = formRecord.getValue();
			@SuppressWarnings("unchecked")
			List<String> row =JsonKit.parse(value,ArrayList.class);
			rows.add(row);
		}
		
		return rows;
	}

	public Activity getActivity(String activityId) {
		Activity activity = Activity.dao.findById(activityId);
		return activity;
	}

	public List<String> getRegistrationTitles(String activityId) {
		// TODO Auto-generated method stub
		List<FormConfig> list = FormConfig.dao.find("select f.fieldName from form_config f where formId =?",activityId);
		List<String> titles = new ArrayList<String>();
		for(FormConfig formConfig:list){
			titles.add(formConfig.getFieldName());
		}
		return titles;
	}


	public void saveActivityInfo(String name, String address, String startTime, String endTime,
			String registrationDeadline,String[] namesss, String[] typesss) {
		Activity activity =new Activity();
		activity.set("name", name).set("address", address).set("startTime", startTime).set("endTime", endTime).set("registrationDeadline", registrationDeadline).save();
		
		for(int i=1;i<namesss.length;i++){
			new FormConfig().set("fieldName", namesss[i]).set("widgetType",typesss[i] ).set("formId", activity.getId()).save();
		}
		
	}

	public List getActivityList() {
		// TODO Auto-generated method stub
		List<Activity> list = Activity.dao.find("select * from activity");
		return list;
	}

	public void deleteActivity(String activityId) {
		// TODO Auto-generated method stub
		new Activity().deleteById(activityId);
	}

}
